/**
 * author:Administrator
 * date:2016/1/7
 * desc:TODO
 */

'use strict';

var request = require('request');

console.log('usage: YEAR=2015 MONTH=10 node checkout_permonth.js');

var __BASE_YEAR = 1900;
var __BASE_MONTH = 1;
var now = new Date();
var year = process.env.YEAR;
var month = process.env.MONTH;
var thisYear = now.getYear() + __BASE_YEAR;
var thisMonth = now.getMonth() + __BASE_MONTH; // 0-based

if (typeof year === 'undefined' || typeof month === 'undefined') {
  console.log('未完全指定 year/month,设置默认值当前时间的 上一个月');
  year = thisYear;
  month = thisMonth - 1;
  if (month <= 0) {
    year = year - 1;
    month = 12;
  }
} else {
  year = parseInt(year);
  month = parseInt(month);
}

console.log();
// 测试时 仅仅 2015-09有数据
year = 2015;
month = 9;

function isValidDate() {
  console.log('当前时间 %s-%s', thisYear, thisMonth);
  console.log('计划结算 %s-%s', year, month);
  if (thisYear < year) {
    return console.log('年份超前:' + year + ' 还不能结算');
  }

  if (thisYear === year && thisMonth < month + 1) {
    return console.log('月份超前:' + month + ' 还不能结算');
  }
  return true;
}

// 本地结算
function localCheckout(cb) {
  // 发送结算请求
  request({
    uri: 'http://localhost:1880/o2o_service/checkout/v1/checkOneMonth',
    method: 'POST',
    json: {
      "year": year,
      "month": month
    }
  }, function (err, res, body) {
    cb(err, body);
  });
}

// 远程资金转账
function remoteCheckout(toBankList, cb) {
  var retList = toBankList; // TODO 发往银行处理,等待结果
  // TODO 如果断网了 怎么办?
  cb(null, retList)
}

// 更新状态
function localUpdateStatus(list, cb) {
  request({
    uri: 'http://localhost:1880/o2o_service/checkout/v1/setStatus',
    method: 'POST',
    json: {
      "id": list,
      "status": "checked"
    }
  }, function (err, res, body) {
    cb(err, body);
  });
}

function doAll() {
  if (!isValidDate()) return;
  localCheckout(function (err, body) {
    if (err) {
      console.log('本地结算 fail:' + err);
      return;
    }
    if (typeof body === 'string') body = JSON.parse(body);
    if (body.status != 200) {
      console.log('本地结算 fail: ' + JSON.stringify(body));
      return;
    } else {
      console.log('本地结算 succ');
    }
    var toCheckList = body.result.checkToday;
    if (toCheckList.length === 0) {
      console.log('结算列表为空');
      return;
    }

    // ----------------------------
    console.log('本地结算列表');
    for (var item of toCheckList) {
      console.log(JSON.stringify(item));
    }
    remoteCheckout(toCheckList, function (err, body) {
      if (err) {
        console.log('资金转账 fail:' + err);
        return;
      }
      if (typeof body === 'string') body = JSON.parse(body);
      //console.log(body);
      //if (body.status != 200) {
      //  console.log('资金转账 fail: ' + JSON.stringify(body));
      //  return;
      //} else {
      //  console.log('资金转账 succ');
      //}
      console.log('资金转账 succ');
      console.log('资金转账返回列表');
      for (var item of body) {
        console.log(JSON.stringify(item));
      }
      var ids = body.map(function (item) {
        return item.id
      });

      // ----------------
      localUpdateStatus(ids, function (err, body) {
        if (err) {
          console.log('本地状态更新 fail:' + err);
          return;
        }
        //console.log(body);
        if (body.status != 200) {
          console.log('本地状态更新 fail: ' + JSON.stringify(body));
          return;
        } else {
          console.log('本地状态更新 succ');
        }
        console.log('月结成功 succ');
      });
    });
  });
}

doAll();